#include "MenuScreen.h"
#include "SpriteMaker.h"
#include "MainGame.h"
#include "2d\CCTransition.h"
USING_NS_CC;

MenuScreen::MenuScreen()
{
}


MenuScreen::~MenuScreen()
{
}

Scene* MenuScreen::createScene()
{
	// 'scene' is an autorelease object
	auto scene = Scene::create();
	// 'layer' is an autorelease object
	auto layer = MenuScreen::create();
	// add layer as a child to scene
	scene->addChild(layer);
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool MenuScreen::init()
{
	//////////////////////////////
	// 1. super init first
	if (!LayerColor::initWithColor(Color4B(172, 240, 255, 255)))
	{
		return false;
	}

	scheduleUpdate();
	spritebatch = SpriteBatchNode::create("menu.png");
	this->addChild(spritebatch);
	cache = SpriteFrameCache::getInstance();
	cache->addSpriteFramesWithFile("menu.plist");

	initCloudy();
	initGlass();
	
	mStartBtn = new MenuButton();
	mStartBtn->init("Menu_Play.png", this,BTN_START);
	return true;
}

void MenuScreen::update(float dt){
	if (mStartBtn->mDoneAction == true)
	{
		stop();
		MainGame::start();
		mStartBtn->mDoneAction = false;
	}

}

void MenuScreen::initCloudy()
{
	for (int i = 0; i < 5; i++)
	{
		int x = rand() % (1280 - 0 + 1) + 500;
	    int y = rand() % (800 - 500 + 1) + 500;
		Sprite* sprite = SpriteMaker::makeSprite(spritebatch, "Menu_Background_2.png");
		sprite->setPosition(Vec2(x, y));
		sprite->setScale((float)i/5);
		Action *action = RepeatForever::create(
			Sequence::create(
			MoveTo::create(rand() % 50-i, Vec2(-250, sprite->getPositionY())),
			MoveTo::create(0, Vec2(1500, sprite->getPositionY())),
			MoveTo::create(rand() % 50-i, Vec2(-250, sprite->getPositionY())),
			nullptr));
		sprite->runAction(action);
	}
}

void MenuScreen::stop()
{

}

void MenuScreen::initGlass()
{

	/////////tree
	//int x,d1,d2, y;
	//float t1, t2, t3;
	//srand(time(NULL));
	//for (int j = 0; j < 10; j++){
	//	int random = rand() % 5;
	//	x = j*random * 300 - Define::SCREEN_WIDTH / 2;
	//	y = 200;
	//	d1 = x + Define::SCREEN_WIDTH / 2;
	//	d2 = 2 * Define::SCREEN_WIDTH - d1;

	//	t1 = (float)d1 * 40 / Define::SCREEN_WIDTH;
	//	t2 = (float)d2 * 40 / Define::SCREEN_WIDTH;

	//	Sprite* sprite = SpriteMaker::makeSprite(spritebatch, "Menu_Background_01.png");
	//	sprite->setPosition(Vec2(x, y));
	//	Action *action = RepeatForever::create(
	//		Sequence::create(
	//		MoveTo::create(t1, Vec2(-Define::SCREEN_WIDTH / 2, sprite->getPositionY())),
	//		MoveTo::create(0, Vec2(Define::SCREEN_WIDTH + Define::SCREEN_WIDTH / 2, sprite->getPositionY())),
	//		MoveTo::create(t2, Vec2(x, sprite->getPositionY())),
	//		nullptr));
	//	sprite->runAction(action);
	//}
	//for (int i = 0; i < 10; i++){
	//	int random = rand() % 5;
	//	x = i*random * 300 - Define::SCREEN_WIDTH / 2;
	//	y = 150;
	//	d1 = x + Define::SCREEN_WIDTH / 2;
	//	d2 = 2 * Define::SCREEN_WIDTH - d1;

	//	t1 = (float)d1 * 20 / Define::SCREEN_WIDTH;
	//	t2 = (float)d2 * 20 / Define::SCREEN_WIDTH;

	//	Sprite* sprite = SpriteMaker::makeSprite(spritebatch, "Menu_Background_01.png");
	//	sprite->setPosition(Vec2(x, y));
	//	Action *action = RepeatForever::create(
	//		Sequence::create(
	//		MoveTo::create(t1, Vec2(-Define::SCREEN_WIDTH/2, sprite->getPositionY())),
	//		MoveTo::create(0, Vec2(Define::SCREEN_WIDTH + Define::SCREEN_WIDTH/2, sprite->getPositionY())),
	//		MoveTo::create(t2, Vec2(x, sprite->getPositionY())),
	//		nullptr));
	//	sprite->runAction(action);
	//}
	//
	/////land
	//Sprite *background = SpriteMaker::makeSprite(spritebatch, "Menu_Background_0.png");
	//background->setPosition(Define::SCREEN_WIDTH / 2, background->getContentSize().height / 2);
	//Action *action1 = RepeatForever::create(
	//	Sequence::create(
	//	MoveTo::create(20, Vec2(-Define::SCREEN_WIDTH / 2, background->getPositionY())),
	//	MoveTo::create(0, Vec2(Define::SCREEN_WIDTH / 2 + Define::SCREEN_WIDTH, background->getPositionY())),
	//	MoveTo::create(20, Vec2(Define::SCREEN_WIDTH / 2, background->getPositionY())),
	//	nullptr));
	//background->runAction(action1);

	//background = SpriteMaker::makeSprite(spritebatch, "Menu_Background_0.png");
	//background->setPosition(Define::SCREEN_WIDTH / 2 + Define::SCREEN_WIDTH, background->getContentSize().height / 2);
	//Action *action2 = RepeatForever::create(
	//	Sequence::create(
	//	MoveTo::create(20, Vec2(Define::SCREEN_WIDTH / 2, background->getPositionY())),
	//	MoveTo::create(20, Vec2(-Define::SCREEN_WIDTH / 2, background->getPositionY())),
	//	MoveTo::create(0, Vec2(Define::SCREEN_WIDTH / 2 + Define::SCREEN_WIDTH, background->getPositionY())),
	//	nullptr));
	//background->runAction(action2);

	///////tree
	int x,d1,d2, y;
	float t1, t2, t3;
	srand(time(NULL));
	for (int j = 2; j < 10; j++){
		int random = rand() % 5;
		x = j*random * 100;
		y = 200;
		Sprite* sprite = SpriteMaker::makeSprite(spritebatch, "Menu_Background_01.png");
		sprite->setPosition(Vec2(x, y));
	}
	for (int i = 0; i < 10; i++){
		int random = rand() % 5;
		x = i*random * 100;
		y = 150;
		Sprite* sprite = SpriteMaker::makeSprite(spritebatch, "Menu_Background_01.png");
		sprite->setPosition(Vec2(x, y));
	}
	
	///land
	Sprite *background = SpriteMaker::makeSprite(spritebatch, "Menu_Background_0.png");
	background->setPosition(Define::SCREEN_WIDTH / 2, background->getContentSize().height / 2);	
}

